const app = getApp();
// pages/management/feedback.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message: "",
    t_length: 0,
    userInfo: {
      userId: "",
      userName: "",
      mobile: ""
    }
  },

  onLoad: function (options) {
    this.init();
  },

  init() {
    // 获取用户信息
    let userInfo = app.globalData.userInfo;
    if (userInfo && userInfo.userDetail) {
      let userDetail = userInfo.userDetail;
      this.setData({
        userInfo: {
          userId: userDetail.id,
          userName: userDetail.realName,
          mobile: userDetail.mobile
        }
      });
    }
  },

  onSubmit() {
    if (!this.data.message) {
      return wx.showToast({
        title: "请输入您的意见",
        icon: "error",
        duration: 1000
      });
    }
    const userInfo = this.data.userInfo;
    wx.showLoading({
      title: "提交中..."
    });
    wx.$apis
      .insertAdvice({
        type: 0,
        advanceType: 2,
        message: this.data.message,
        mobile: userInfo.mobile,
        userId: userInfo.userId,
        userName: userInfo.userName
      })
      .then((res) => {
        wx.showToast({
          title: "提交成功，感谢您的反馈",
          icon: "none",
          duration: 2000
        }).then((r) => {
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        });
      });
  }
});
